<?php
class Uuch_Express_NiuMen
{
    private $_html;
    public function __construct($name, $expressNo) {
        $html = file_get_contents('http://211.162.73.133:1356/cgi-bin/GInfo.dll?EmmisTrackGenData&cemskind='.$name.'&cno=' . $expressNo);
        $this->_html = iconv('GB2312', 'UTF-8', $html);
    }

    private function getNum()
    {
        $html = $this->_html;
        $statusnum = stripos($html,"\n");
        $infonum = stripos($html, "<TRACK_DATA>");
        return array('statusnum'=>$statusnum,'infonum'=>$infonum);
    }
    private function AnalysisStatus()
    {
        $html = $this->_html;
        if($html == "-1"){
            return 0;
        }
        $arrnum = $this->getNum();
        $status = substr($html,0,$arrnum["statusnum"]);
        
        return $status;
    }
    private function AnalysisInfo()
    {
        $html = $this->_html;
        if($html == "-1"){
            return 0;
        }
        $arrnum = $this->getNum();
        $info = substr($html,$arrnum["statusnum"], $arrnum["infonum"]);
        $info = str_replace("\r","",str_replace("\n", "",$info));
        $p = xml_parser_create();
        xml_parse_into_struct($p, $info, $vals, $index);
        xml_parser_free($p);
        $return = new Uuch_Express_Info();
        foreach ($vals as  $val) {
            if($val["tag"] != "EMS_INFO"){
                $rekey = $val["tag"];
                if(isset($val["value"])){
                    $return->$rekey = $val["value"];
                } else {
                    $return->$rekey = "";
                }
            }
        }
        return $return;
    }
    private function AnalysisTrack()
    {
        $html = $this->_html;
        if($html == "-1"){
            return 0;
        }
        $arrnum = $this->getNum();
        $track = substr($html, $arrnum["infonum"]);
        $track = str_replace("\r","",str_replace("\n", "",$track));
        
        $p = xml_parser_create();
        xml_parse_into_struct($p, $track, $vals, $index);
        xml_parser_free($p);
        $return = array();
        $i = 0;
        foreach ($vals as $key =>  $val) {
            if($key == 0 || $key % 3 == 1){
                $i++;
                $track = new Uuch_Express_Track();
            }
            if($val["tag"] != "TRACK_DATA"){
                $rekey = $val["tag"];
                if(isset($val["value"])){
                    $track->$rekey = $val["value"];
                } else {
                    $track->$rekey = "";
                }
                $return[$i] = $track;
            }
        }
        return $return;
    }

    public function getStatus()
    {
        $info = $this->AnalysisStatus();
        return $info;
    }
    
    public function getInfo()
    {
        $info = $this->AnalysisInfo();
        return $info;
    }
    
    public function getTrackData(){
        $trackData = $this->AnalysisTrack();
        return $trackData;
    }
}

